package com.paomi.client.activity.address;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.R;
import com.paomi.client.activity.address.Adapter.AddressListAdapter;
import com.paomi.client.activity.address.View.ReceivingAddressView;
import com.paomi.client.activity.address.presenter.ReceivingAddressPre;
import com.paomi.client.activity.pay.model.AddressBean;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.AddressInfo;
import com.paomi.client.bean.AddressListInfo;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class ReceivingAddressActivity extends BaseMvpActivity<ReceivingAddressView, ReceivingAddressPre> implements ReceivingAddressView {
    @BindView(R.id.tv_add_address)
    TextView tv_add_address;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.button_goback)
    TextView button_goback;

    AddressListAdapter adapter;

    List<AddressInfo> addressList = new ArrayList<>();

    @NonNull
    @Override
    public ReceivingAddressPre createPresenter() {
        return new ReceivingAddressPre();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.receive_address);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_receiving_address;
    }

    @OnClick(R.id.tv_add_address)
    void add() {
        Intent i = new Intent(this, AddAddressActivity.class);
        startActivity(i);
    }

    @Override
    protected void onResume() {
        super.onResume();
        addressList.clear();
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", "1");
        map.put("pageSize", "" + Constants.PAGE_SIZE);
        getPresenter().getAddressNum(map);
    }

    @Override
    protected void initEvent() {
        button_goback.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddressListAdapter(this, iconfont);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new AddressListAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClick(int pos) {
                Intent intent = new Intent();
                intent.putExtra("getAddress", addressList.get(pos));
                setResult(2018, intent);
                finish();
            }

            @Override
            public void delete(String id, int pos) {
                getPresenter().deleteAddress(id, pos);
            }
        });
    }

    @Override
    public void showAddressList(AddressListInfo s) {
        if (s.returnCode.equals("0000")) {
            if (s.getReturnData() != null) {
                if (s.getReturnData().getData().size() == 0) {
                } else {
                    AddressInfo info = null;
                    addressList.addAll(s.getReturnData().getData());
                    for (int i = 0; i < addressList.size(); i++) {
                        if (addressList.get(i).isDefault()) {
                            info = addressList.remove(i);
                        }
                    }
                    if (info != null) {
                        addressList.add(0, info);
                    }
                    adapter.setData(addressList);
                }
            }
        } else {
            Util.toast(this, s.returnMessage);
        }
    }

    @Override
    public void deleteSuccess(BaseJSON result, int pos) {
        if (result.returnCode.equals("0000")) {
            addressList.remove(pos);
            adapter.notifyDataSetChanged();
        } else {
            Util.toast(this, result.returnMessage);
        }
    }
}
